package servlet.Room;

import domain.Community;
import domain.Page;
import domain.Room;
import domain.User;
import service.community.CommunityService;
import service.community.CommunityServiceImpl;
import service.room.RoomService;
import service.room.RoomServiceImpl;
import util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class RoomInfo extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        int everyPage = 15;//每页记录数
        RoomService roomService = new RoomServiceImpl();
        User loginUser = (User)request.getSession().getAttribute("adminloginflag");
        if (loginUser == null)
            request.getRequestDispatcher("/admin/index.jsp").forward(request, response);
        else {
            String view_power = loginUser.getView_power();
            int totalCount = roomService.getCount(view_power);//获取总记录数
            //点击链接重新获取当前页
            String scurrentPage = request.getParameter("currentPage");
            int currentPage = 1; //当前页，默认1
            if (scurrentPage == null) {
                currentPage = 1;//从第一页开始访问
            } else {
                currentPage = Integer.parseInt(scurrentPage);
            }
            //分页信息
            Page page = PageUtil.createPage(everyPage, totalCount, currentPage);
            //分页数据信息
            ArrayList<Room> list = roomService.findByPage(page,view_power);

            request.setAttribute("page", page);
            request.setAttribute("list", list);
            if(request.getAttribute("message")!=null) {
                request.setAttribute("message",request.getAttribute("message"));
            }
            request.getRequestDispatcher("/admin/Room/index.jsp").forward(request, response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
